package com.apexsoft.assets.liveservice.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONObject;
import com.apexsoft.*;
import com.apexsoft.assets.base.param.FileFrameWithParam;
import com.apexsoft.assets.base.param.TempFile;
import com.apexsoft.assets.base.response.AResponse;
import com.apexsoft.assets.base.utils.ResultUtil;
import com.apexsoft.assets.liveservice.ZgTestService;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Map;


@LiveService( note = "资管测试")
@Slf4j
public class ZgTestServiceImpl implements ZgTestService {

    @Override
    public AResponse sayHello(Map<String,Object> param){
        String note = MapUtil.getStr(param,"name");
        return ResultUtil.successNote(note);
    }

    @Override
    public RequestObserver<FileFrameWithParam> uploadFile(ResponseObserver<AResponse> responseObserver) {
        return new RequestObserver<FileFrameWithParam>() {
            TempFile tempFile;
            @Override
            public void onNext(FileFrameWithParam value) throws Exception {
                if(value.isMetadata()){
                    JSONObject param = new JSONObject(value.getParam());
                    //业务处理
                    //...
                    //直接抛出异常
                    throw new LiveServiceException("ERROR");
                    /*if(log.isDebugEnabled()){
                        log.debug(param.toString());
                    }
                    tempFile = new TempFile();
                    tempFile.setFileName(value.getFileName());
                    tempFile.toWriteState();*/
                }else{
                    tempFile.write(value.getBytes(),value.getBytes().length);
                }
            }

            @Override
            public void onError(Throwable t) {
                log.error(t.getMessage());
                if(tempFile!=null){
                    try {
                        tempFile.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onCompleted() throws Exception {
                if(tempFile!=null){
                    try{
                        tempFile.toReadState();
                        //业务处理
                        //...
                    }finally {
                        tempFile.close();
                    }
                }
                responseObserver.onNext(ResultUtil.successNote("上传成功"));
                responseObserver.onCompleted();
            }
        };
    }

    @Override
    public RequestObserver<String> downloadFile(ResponseObserver<FileFrame> responseObserver) {
        return new RequestObserver<String>() {
            File file;
            //参数
            JSONObject param;

            @Override
            public void onNext(String value) {
                //接收的参数
                param = new JSONObject(value);
            }

            @Override
            public void onError(Throwable t) {
                //接收消费者反馈回来的异常
                log.error(t.getMessage(), t);
            }

            @Override
            public void onCompleted() throws Exception {
                //业务处理
                //...
                if(log.isDebugEnabled()){
                    log.debug(param.toString());
                }
                //测试用
                file = new File("1-aa.txt");
                if (!file.exists()) {
                    throw new Exception("文件不存在");
                } else {
                    try (FileInputStream fis = new FileInputStream(file)) {
                        FileFrame fileFrame = new FileFrame("1-ocr.txt", fis.available(), "");
                        responseObserver.onNext(fileFrame);
                        //故意写这么小，用于测试实际使用不要这么小
                        byte[] bytes = new byte[28];
                        int length;
                        while ((length = fis.read(bytes)) != -1) {
                            fileFrame = new FileFrame(bytes, length);
                            responseObserver.onNext(fileFrame);
                        }
                        responseObserver.onCompleted();
                    } catch (Exception e) {
                        throw e;
                    }
                }
            }
        };
    }
}